//create by giaotk
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
namespace Bussiness
{
    public class MoneyDispenserBLL : IMoneyDispenserBLL
    {
        #region IMoneyDispenserBLL Members by Giaotk
        public bool DispenseMoney(float amount)
        {
            MoneyDispenser moneyDispenser = new MoneyDispenser();
            AtmSystem atmsystem = new AtmSystem();
            moneyDispenser.TotalMoney = amount;
            //  Check if the amount can be dispensed with 20k notes but not a 50k.
            if ((amount % 20 == 0) && amount % 50 != 0)
            {
                while (amount >= 20)
                {
                    //  Adds oen to the number of 20k notes.
                    moneyDispenser.twentyNotes += 1;

                    //  Lowers the remaining amount by 20k.
                    amount = amount - 20;
                }
            }
            //  Dispenses a 50k note 
            if (amount % 50 == 0)
            {
                while (amount >= 50)
                {
                    //  Increases the number of 50k notes.
                    moneyDispenser.fiftyNotes += 1;
                    //  Lowers the remaining amount by 50k.
                    amount = amount - 50;
                }
            }
            else
            {
                //  While the amount is above 50k and the remainder 
                //  can't be made up of 20k bills, add to the fifty
                //  thousand vnd note count.
                while ((amount >= 50) && (amount % 20 != 0))
                {
                    amount = amount - 50;
                    moneyDispenser.fiftyNotes += 1;
                }

                //  If the remainder can be compiled of 20k bills,
                //  add to the twenty thousand vnd note count.
                if (amount % 20 == 0)
                {
                    while (amount >= 20)
                    {
                        amount = amount - 20;
                        moneyDispenser.twentyNotes += 1;
                    }
                }
            }

            //  Call the SuccessfulWithdrawal method to check that
            //  the amount can be withdrawn in 20's or 50's.
            if (amount <= 0) return false;
            else
                return SuccessfulWithdrawal(moneyDispenser.fiftyNotes, moneyDispenser.twentyNotes, atmsystem);
        }
        public bool SuccessfulWithdrawal(int fiftyNotes, int twentyNotes, AtmSystem atmsystem)
        {
            if (atmsystem.twentyNotes > twentyNotes && atmsystem.fiftyNotes > fiftyNotes)
            {
                atmsystem.twentyNotes -= twentyNotes;
                atmsystem.fiftyNotes -= fiftyNotes;
                if ((twentyNotes * 20 + fiftyNotes * 50) <= 50)
                    return false;
                else return true;
                //ATM dispense money...
            } 
            else return false;
        }
        public int CheckMoney()
        {
            return 0;
        }
        public int ManagerNoteInside()
        {
            return 0;
        }

        #endregion
    }
}
